Optimize a function#
[1]:
# import libraries
import jax.numpy as jnp
from optymus import Optimizer
from optymus.benchmark import MccormickFunction
[2]:
# define the objective function
f = MccormickFunction()
# define initial guess
initial_point = jnp.array([-3.0, 2.0])
[3]:
# instantiate the optimizer with the objective function and the initial point
# select the method to be used
opt = Optimizer(
f_obj=f,
x0=initial_point,
method='newton_raphson',
h_type='hessian',
)
Newton-Raphson 0: 3%|▎ | 3/100 [00:01<00:53, 1.81it/s]
[4]:
# print a short report about the optimization process
opt.print_report()
[4]:
| Method | Initial Guess | Optimal Solution | Objective Function Value | Number of Iterations | Time Elapsed | |
|---|---|---|---|---|---|---|
| Optimization Results | newton_raphson | [-3.0, 2.0] | [-0.5471975467892413, -1.5471975535583] | -1.9132229549810367 | 3 | 1.658297 |
[5]:
# plot the results
opt.plot_results(min=-5, max=5, show=True, notebook=True)